package com.lunasoft.client;

import com.lunasoft.core.Constants;
import com.lunasoft.core.LoginMsg;
import com.lunasoft.core.PingMsg;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

import java.util.concurrent.TimeUnit;

/**
 * @author Jierry
 * @version 2016/7/11.
 */
public class NettyClient {
    private static final EventExecutorGroup group = new DefaultEventExecutorGroup(20);
    private int port;
    private String host;
    private SocketChannel socketChannel;

    private void start(int port, String host) throws InterruptedException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        Bootstrap b = new Bootstrap()
                .channel(NioSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE,true)
                .group(eventLoopGroup)
                .remoteAddress(host,port)
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ChannelPipeline p = socketChannel.pipeline();
                        p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        p.addLast(new ObjectEncoder());
                        p.addLast(new IdleStateHandler(20,10,0));
                        p.addLast(new NettyClientHandler());
                    }
                });
        ChannelFuture f = b.connect(host,port).sync();
        if (f.isSuccess()){
            socketChannel = (SocketChannel)f.channel();
            System.out.println("connect server  成功---------");
        }
    }

    public static void main(String[]args) throws InterruptedException {
        //Constants.setClientId(UUID.randomUUID().toString());
        Constants.setClientId("001");
        int port = 9090;
        String host = "127.0.0.1";
        NettyClient client = new NettyClient();
        client.start(port,host);

        LoginMsg loginMsg=new LoginMsg();
        loginMsg.setPassword("admin123");
        loginMsg.setUserName("admin");
        client.socketChannel.writeAndFlush(loginMsg);
        while (true){
            TimeUnit.SECONDS.sleep(3);
//            AskMsg askMsg=new AskMsg();
//            AskParams askParams=new AskParams();
//            askParams.setAuth("authToken");
//            askMsg.setParams(askParams);
//            client.socketChannel.writeAndFlush(askMsg);
            PingMsg pingMsg = new PingMsg();
            client.socketChannel.writeAndFlush(pingMsg);
        }
    }
}
